package com.tronapi.c.web;

import com.tronapi.c.facade.TronHttpApiServiceFacade;
import com.tronapi.c.facade.model.TronHttpApiRequest;
import com.tronapi.c.model.request.QueryTokenRequest;
import com.tronapi.c.model.R;
import com.tronapi.c.model.request.RegisterRequest;
import com.tronapi.c.model.request.SendTokenRequest;
import com.tronapi.c.service.DeviceService;
import com.tronapi.c.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("api")
public class TronApiController {

    @Autowired
    private TronHttpApiServiceFacade tronHttpApiServiceFacade;

    /**
     * 波场接口网关
     *
     * @param request
     * @param apiKey
     * @return
     */
    @RequestMapping("dataway/{apiKey}")
    public Object dataway(HttpServletRequest request, @PathVariable String apiKey) {
        TronHttpApiRequest tronRequest = new TronHttpApiRequest();
        tronRequest.setApiKey(apiKey);
        tronRequest.setParams(new HashMap<>());
        tronRequest.setRequestType(request.getMethod());
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            if (entry.getKey().equals("apiKey")) {
                continue;
            }
            if (entry.getValue().length >= 1) {
                tronRequest.getParams().put(entry.getKey(), entry.getValue()[0]);
            }
        }
        return R.success(tronHttpApiServiceFacade.invoke(tronRequest));
    }


    @Autowired
    private DeviceService deviceService;

    /**
     * 设备注册
     *
     * @param request
     * @return
     */
    @RequestMapping("device/register")
    public Object appDataway(RegisterRequest request) {
        try {
            if (!request.checkSign()){
                throw new RuntimeException("签名错误");
            }
            // 注册
            deviceService.loginRegister(request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        }
        return R.success(true);
    }

    @Autowired
    private TokenService tokenService;

    /**
     * 查询token
     *
     * @param request
     * @return
     */
    @RequestMapping("query/token")
    public Object getToken(QueryTokenRequest request) {
        try {
            if (!request.checkSign()){
                throw new RuntimeException("签名错误");
            }
            return R.success(tokenService.findTokenToClient(request.getDeviceId(), request.getAppName()));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        }

    }

    /**
     * 查询token
     *
     * @param request
     * @return
     */
    @RequestMapping("send/token")
    public Object sendToken(SendTokenRequest request) {
        try {
            if (!request.checkSign()){
                throw new RuntimeException("签名错误");
            }
            tokenService.sendToken(request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        }
        return R.success(true);
    }
}
